package cn.jofei.client.abstra;

import cn.jofei.common.ProxyInfo;
import cn.jofei.common.config.AppProperties;
import cn.jofei.common.enums.Types;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 客户机
 *
 * @author Jofei Bao
 * @version ngbao v1.0.1
 * @date 2022年02月26日 14:28
 */
public abstract class Client implements Callable<Object> {
    public static final Logger log = LoggerFactory.getLogger(Client.class);

    /**
     * 注册信息
     */
    public static ConcurrentHashMap<String, ProxyInfo> REG_INFOS = new ConcurrentHashMap<>();

    /**
     * 放入本地注册成功队列
     *
     * @param info
     */
    public static void register(ProxyInfo info) {
        String remoteIP = AppProperties.get("server.domain");
        if (Types.TCP.eq(info.getType())) {
            REG_INFOS.putIfAbsent(info.getRemotePort() + "", info);
            log.info("\r\n\r\n注册 tcp 成功: \r\n[{} --> {}] \r\n", remoteIP + ":" + info.getRemotePort(), info.getLocalIP() + ":" + info.getLocalPort());
        } else if (Types.WEB.eq(info.getType())) {
            REG_INFOS.putIfAbsent(info.getSubdomain(), info);
            String[] s = {
                    "http://" + info.getSubdomain() + "." + remoteIP,
                    "http://" + info.getLocalIP() + ":" + info.getLocalPort(),
                    "https://" + info.getSubdomain() + "." + remoteIP,
                    "https://" + info.getLocalIP() + ":" + info.getLocalPort()};
            log.info("\r\n\r\n注册 http 成功: \r\n[{} --> {}]\r\n[{} --> {}]\r\n", s);
        }
    }
}
